package com.emm.yixun.mobile.ui.analysis;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.base.BaseActivity;
import com.emm.yixun.mobile.customview.BarCharts;
import com.emm.yixun.mobile.model.GetPaymentNotSignedList;
import com.emm.yixun.mobile.model.GetPaymentNotSignedList.PaymentModeList;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import xf.tools.Loading;

/**
 * 
 * <p>
 * <b>Title：</b> 已签约未回款
 * </p>
 * <p>
 * <b>Description：</b>
 * </p>
 * 
 * @author tangxin
 * @date 2016年12月6日 下午4:12:19
 */
public class NotSignedBackActivity extends SwipeBackActivity {

	private static final String TAG = "NotSignedBackActivity";

	LinearLayout linear;

	private BarChart mBarChart;
	private BarData mBarData;
	private BarCharts mBarCharts;

	TextView title_main;

	ImageView back_btn;

	TextView all_price;

	/** 侧滑关闭框架 */
	private SwipeBackLayout mSwipeBackLayout;
	
	@Override
	protected void onResume() {
		BaseActivity.activity = this; // TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.notsignedback_activity);
		
		SetSwipeBackLayout(mSwipeBackLayout);
		
		BaseActivity.activity = this;

		initView();
		
		SetRelativiLayoutPadding(R.id.main_head_top_rela);
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 初始化view
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 下午4:18:59
	 */
	private void initView() {
		linear = (LinearLayout) findViewById(R.id.linear);
		mBarChart = (BarChart) findViewById(R.id.spreadBarChart);
		mBarCharts = new BarCharts();
		title_main = (TextView) findViewById(R.id.title_main);
		title_main.setText("已签约未回款");
		back_btn = (ImageView) findViewById(R.id.back_btn);
		all_price = (TextView) findViewById(R.id.all_price);
		initBtn();
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 初始化监听事件
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 下午4:19:09
	 */
	private void initBtn() {
		back_btn.setVisibility(View.VISIBLE);
		back_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		getPaymentNotSignedList();

	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 绘制图表
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @param size
	 * @param getpayment
	 * @author tangxin
	 * @date 2016年12月7日 下午4:20:02
	 */
	public void SetBarCharts(int size, GetPaymentNotSignedList getpayment) {
		mBarData = mBarCharts.getBarData(size, getpayment, "已签约未回款");
		mBarCharts.showBarChart(mBarChart, mBarData, "已签约未回款统计");
	}

	/**
	 * 查询已签约未回款分析
	 */
	GetPaymentNotSignedList getpayment;

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 查询已签约未回款分析
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 下午4:20:26
	 */
	private void getPaymentNotSignedList() {
		EmmApplication.updateUrl("getPaymentNotSignedList");
		Map<String, String> map = new HashMap<String, String>();
		map.put("version", EmmApplication.getPackageManager(5));
		map.put("merchantId", EmmApplication.getData(EmmApplication.merchantId));
		map.put("token", EmmApplication.getToken());
		map.put("userId", EmmApplication.getUserId());
		map.put("projectCode", EmmApplication.getProjectCode());
		map.put("terminalCode", EmmApplication.getPackageManager(2));
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("params", map);
		JSONObject json = new JSONObject(paramsMap);
		String jsonstr = json.toString();
		RequestParams params = new RequestParams();
		params.put("body", jsonstr);
		Log.v("getPaymentNotSignedList-->post", EmmApplication.urlStr + "?body=" + jsonstr);
		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.post(EmmApplication.urlStr, params, new AsyncHttpResponseHandler() {
			@Override
			public void onFailure(Throwable error, String content) {
				super.onFailure(error, content);
				Loading.hideDialogForLoading();
				Log.v(TAG, "请求结束error:" + error + "==" + content);
			}

			@Override
			public void onFinish() {
				Loading.hideDialogForLoading();
				Log.v(TAG, "请求结束");
				super.onFinish();
			}

			@Override
			public void onStart() {
				super.onStart();
				Loading.showDialogForLoading(NotSignedBackActivity.this, "正在加载...", false, true, 60000);
				Log.v(TAG, "请求开始");
			}

			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				if (content == null) {
					return;
				}
				Log.v(TAG, content);
				JSONObject jsonObj = JSONObject.parseObject(content);
				JSONObject resData = (JSONObject) jsonObj.get("resData");
				if (resData == null) {
					Log.v(TAG, "信息返回值为空");
					return;
				}

				getpayment = JSONObject.parseObject(resData.toString(), GetPaymentNotSignedList.class);

				if ("1".equals(getpayment.getResult())) {
					Log.v("获取成功", "");
					String str = EmmApplication.isNull(getpayment.getTotalAmount()) == true ? getpayment.getTotalAmount() : "0";
					all_price.setText(EmmApplication.drop2(str) + "万");
					if (null != getpayment.getPaymentModeList() && getpayment.getPaymentModeList().size() > 0) {
						SetBarCharts(getpayment.getPaymentModeList().size(), getpayment);
					} else {
						EmmApplication.Ts("当前没有统计数据");
						ArrayList<PaymentModeList> paymentModeList = new ArrayList<GetPaymentNotSignedList.PaymentModeList>();
						PaymentModeList p = new PaymentModeList();
						p.setPaymentModeAmount("0");
						p.setPaymentModeName(" ");
						paymentModeList.add(p);
						getpayment.setPaymentModeList(paymentModeList);
						SetBarCharts(paymentModeList.size(), getpayment);
					}
				} else {
					EmmApplication.T(getpayment.getErrorMsg().toString());
					Log.v("获取失败", "errorCode:" + getpayment.getErrorCode().toString() + "errorMsg:" + getpayment.getErrorMsg().toString());
				}
			}
		});
	}

}
